package com.bjesc_b2c.app.appraiset.mvp.view.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bjesc_b2c.app.appraiset.R;
import com.bjesc_b2c.app.appraiset.base.BaseFragment;
import com.bjesc_b2c.app.appraiset.mvp.view.activity.CarDelailsActivity;
import com.bjesc_b2c.app.appraiset.mvp.view.activity.DemandCarActivity;
import com.facebook.drawee.view.SimpleDraweeView;
import com.rxretrofit_build.model.RxBusBean;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import master_bean.CustomerDetailsBean;

import static com.bjesc_b2c.app.appraiset.R.id.tv_car_similar;

/**
 * Created by yidong on 2017/5/25.
 */

public class DemandCarFragment extends BaseFragment {
    @InjectView(R.id.iv_car_photo)
    SimpleDraweeView simpleCarPhoto;
    @InjectView(R.id.tv_car_type)
    TextView carTypeTv;
    @InjectView(R.id.tv_car_price)
    TextView carPriceTv;
    private DealerDialogListener dialogListener;
    private CustomerDetailsBean.DemandCarBean demandCarBean;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            demandCarBean= (CustomerDetailsBean.DemandCarBean)getArguments().getSerializable("DemandCar");
        }
    }
    @Override
    public void onAttach(Activity activity){
        super.onAttach(activity);
        try{
            dialogListener =(DealerDialogListener)activity;
        }catch(ClassCastException e){
            throw new ClassCastException(activity.toString()+"must implement DealerDialogListener");
        }
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        contentView = inflater.inflate(R.layout.fragment_demand_car,null);
        ButterKnife.inject(this, super.onCreateView(inflater, container, savedInstanceState));
        simpleCarPhoto.setImageURI(demandCarBean.getImg());
        carTypeTv.setText(demandCarBean.getTitle());
        carPriceTv.setText(demandCarBean.getOwner_price());
        return super.onCreateView(inflater, container, savedInstanceState);
    }
//    @Override
//    public void onHiddenChanged(boolean hidden) {
//        super.onHiddenChanged(hidden);
//        if (!hidden) {
//            if (getArguments() != null) {
//                demandCarBean= (CustomerDetailsBean.DemandCarBean)getArguments().getSerializable("DemandCar");
//                simpleCarPhoto.setImageURI(demandCarBean.getImg());
//                carTypeTv.setText(demandCarBean.getTitle());
//                carPriceTv.setText(demandCarBean.getOwner_price());
//            }
//
//        }
//    }
@Override
protected void doOnNext(RxBusBean rxBusBean) {
    super.doOnNext(rxBusBean);
    if (rxBusBean.getRequestMark().equals("DemandCar")){
        demandCarBean = (CustomerDetailsBean.DemandCarBean)rxBusBean.getO();
        if (demandCarBean != null){
            simpleCarPhoto.setImageURI(demandCarBean.getImg());
            carTypeTv.setText(demandCarBean.getTitle());
            carPriceTv.setText(demandCarBean.getOwner_price());
        }
    }
}
    @OnClick({R.id.lin_car_info,R.id.tv_look_dealer,R.id.tv_sms_share,R.id.tv_car_maintain, tv_car_similar})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.lin_car_info:
                startIntent(new Intent(getActivity(),CarDelailsActivity.class));
                break;
            case R.id.tv_look_dealer:
                if(dialogListener !=null){
                    dialogListener.createDiaog();
                }
                break;
            case R.id.tv_car_similar:
//                startIntent(new Intent(getActivity(),LikeDealersActivity.class));
               Intent mIntent = new Intent(getActivity(),DemandCarActivity.class);
                mIntent.putExtra("DATATYPE",2);
                startIntent(mIntent);
                break;
        }

    }
    public interface DealerDialogListener{
        void createDiaog();
    }
}
